Ext.namespace("Ext.sw");
Ext.sw.CheckBox = function(config){
	config = config || {}
	Ext.sw.CheckBox.superclass.constructor.apply(this,arguments);
}
Ext.extend(Ext.sw.CheckBox,Ext.form.Checkbox,{
	reset:function(){
		//do not
	}
})
//url columns
Ext.sw.FieldSetCheckBox = function(config){
	config = config || {}
	if(config.store){
		this.store = config.store;
	}
	if(config.checkId){
		this.checkId = config.checkId;
	}
	if(config.checkName){
		this.checkName = config.checkName;
	}
	if(config.subName){
		this.subName = config.subName;
	}
	if(config.lazyinit){
		this.lazyinit = config.lazyinit;
	}
	config.layout="table";
	Ext.sw.FieldSetCheckBox.superclass.constructor.apply(this,arguments);
}

Ext.extend(Ext.sw.FieldSetCheckBox,Ext.form.FieldSet,{
	store:null,
	lazyinit:false,
	checkId:"test",
	checkName:"test",
	subName:"test",
	markInvalid:function(){},
	clearInvalid:function(){},
	originalValue:null,
	initComponent : function(){
        Ext.sw.FieldSetCheckBox.superclass.initComponent.call(this);
        if(!this.lazyinit){
        	this.doLoad();
        }
	},
	onRender : function(ct, position){
        Ext.sw.FieldSetCheckBox.superclass.onRender.call(this, ct, position);
        if(this.lazyinit){
        	this.doLoad();
        }
        this.doRender();
	},
	doLoad:function(){
		this.store.load();
	},
	doRender:function(){
		for(var i=0;i<this.store.getCount();i++){
			var record = this.store.getAt(i);
			this.addCheckBox(record,i);
		}
	},
	addCheckBox:function(record,i){
		var id = record.get(this.checkId);
		var name = record.get(this.checkName);
		var checkbox = new Ext.sw.CheckBox({
				id:id,
				name:this.subName.replace("${index}",i),
				boxLabel:name,
				inputValue:id
				});
		this.add(checkbox)
	},
	setValue:function(val){
		this.clean();
		if(this.isEmpty(val) && !Ext.isArray(val)){
			return;
		}
		var cache = [];
		this.fullCache(cache,val);
		for(var i=0;i<cache.length;i++){
			var cc = this.get(cache[i]);
			if(cc){
				cc.setValue(true);
			}
		}
		this.originalValue = val;
	},
	fullCache:function(cache,val){
		for(var i=0;i<val.length;i++){
			if(Ext.isArray(val[i])){
				fullCache(cache,val[i]);
			}
			cache.push(val[i]);
		}
	},
	getValue:function(){
		return this.find('checked',true);
	},
	isEmpty:function(val){
		if(val == null || val == "" || val == 'undefined'){
			return true;
		}
		return false;
	},
	validate:function(){
		return true;
	},
	reset:function(){
		this.setValue(this.originalValue);
	},
	clean:function(){
		if(this.items){
			this.items.each(function(obj){
				obj.setValue(false);
			});
		}
	}
})

